package com.gzy.rpsm_admin.controller;

import com.gzy.rpsm_admin.pojo.PageResult;
import com.gzy.rpsm_admin.pojo.ResponsePojo;
import com.gzy.rpsm_admin.pojo.user.LoginUser;
import com.gzy.rpsm_admin.pojo.user.ResUser;
import com.gzy.rpsm_admin.pojo.user.User;
import com.gzy.rpsm_admin.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Optional;
@RestController
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/admin/login")
    @ApiOperation( value="根据账号和密码进行登录操作，只需账号和密码。 返回个人信息和 token 字符串",
            notes="<span style='color:red'>模拟操作，密码为明文密码 </span>",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name="account",type =
                    "String",required = true),
            @ApiImplicitParam(name="password",type="String",required =
                    true)
    })
    @ApiParam

    public ResponsePojo<ResUser> login(@ApiIgnore LoginUser
                                               user){
        user.setUser_type((short)1);
        ResUser result = userService.login(user);
        if (Optional.ofNullable(result).isPresent()) {
            return ResponsePojo.success(result);
        }else{
            return ResponsePojo.fail(null,"验证失败");
        }
    }
    @GetMapping("/Page/user")
    @ApiOperation(value = "返回用户分页数据")
    @ApiParam
    @ApiImplicitParams({
            @ApiImplicitParam(name="current",value ="当前页面",dataType = "Integer",paramType = "query",defaultValue = "1"),
            @ApiImplicitParam(name="size",value = "页面行数",dataType = "Integer",paramType = "query",defaultValue = "10"),
            @ApiImplicitParam(name="condition",value = "姓名或身份证的查 询关键字",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name="user_type",value = "用户类型(1、管理员；2、物业；3、业主；)",dataType = "Integer",required = true)
    })
    public ResponsePojo<PageResult<User>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            String condition,@RequestParam(defaultValue = "10")
            Integer size,
            @RequestParam(required = true) Integer user_type){
        PageResult<User> users =
                userService.pageOfUser(current,size,condition,user_type);
        ResponsePojo<PageResult<User>> user=
                ResponsePojo.success(users);
        return user;
    }
    @GetMapping("/admin/byid")
    @ApiOperation( value="根据 id 查找单个用户",
            notes="<span style='color:red'>根据用户 id 查询用户信息 </span>",
            response = User.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({ //非实体类参数
            @ApiImplicitParam(name="id",value ="用户 id",dataType = "int",paramType = "query")
    })
    @ApiParam //实体类参数
    public ResponsePojo<User> queryById(@RequestParam(required = true) Integer id){
        User u = userService.searchById(id);
        return ResponsePojo.success(u);
    }
}
